<?php

require_once 'common/conn.php';
$result = array();
$usercode = $_GET['usercode'];
if (!isset($usercode)) {
    $usercode = $_POST['usercode'];
}
$monthlyScore = $_POST['monthlyScore'];
$dailyScore = $_POST['dailyScore'];
$month = date("Y-m");

$db = new DBC();
if (isset($monthlyScore)) {
    $data = json_decode($monthlyScore, true);
    $sql = "INSERT INTO t_device_degree_monthly (deviceid,thinking,habit,art,language,cognition,gainMonth,createTime) VALUES((SELECT deviceid FROM t_device WHERE devicecode ='" . $usercode . "')," . $data['thinking'] . "," . $data['habit'] . "," . $data['art'] . "," . $data['language'] . "," . $data['cognition'] . ",'" . $month . "',now())";

    $id = $db->insertId($sql);
    if ($id > 0) {
        $result['errorNo'] = 0;
    } else {
        $result['errorNo'] = 1;
    }
}

if (isset($dailyScore)) {
    $data = json_decode($dailyScore, true);
    $daily = $data['dailyScore'];
    foreach ($daily as $value) {
        $sql = "INSERT INTO t_device_degree_daily (deviceid,thinking,habit,art,language,cognition,gainDate,createTime) VALUES((SELECT deviceid FROM t_device WHERE devicecode ='" . $usercode . "')," . $value['thinking'] . "," . $value['habit'] . "," . $value['art'] . "," . $value['language'] . "," . $value['cognition'] . ",'" . $value['date'] . "',now())";

        $id = $db->insertId($sql);
        if ($id > 0) {
            $result['errorNo'] = 0;
        } else {
            $result['errorNo'] = 1;
        }
    }
}
echo json_encode($result);
?>
